#!/bin/sh
#
# This is the stop  start script for dkim milter
#
# on Redhat style machines copy to /etc/init.d/milter
#


function start() { 
    echo "Starting dkim"
    /opt/dkim/dkim_milter  /opt/dkim/dkim.conf
    sleep 1
    chmod 777 /var/run/dkim/f1.sock
}

function stop() { 
 echo "Stopping dkim"
 cat   /var/run/dkim/dkim.pid 2>/dev/null | xargs --no-run-if-empty kill
 sleep 1
 rm -f /var/run/dkim/f1.sock
}

if [ "$1" = "start" ];then
start
exit
fi


if [ "$1" = "stop" ];then
stop
exit
fi

if [ "$1" = "restart" ];then
stop
sleep 1
start
exit
fi

if [ "$1" = "status" ];then
lsof  /var/run/dkim/f1.sock
exit
fi

if [ "$1" = "check" ];then
X=$(lsof -t /var/run/dkim/f1.sock 2>/dev/null)
if [ -z "$X" ];then
start
else

pmap $(cat /var/run/dkim/dkim.pid ) | tail -n 1
echo $(date) " Running"

fi
exit
fi


